<?php
/**
 *问答社区答案页控制器
 */
class Commwapanswer extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('commwap_model');
    }
    public function checkAnswerExist()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $res = $this->commwap_model->checkAnswerExist($qid,$uid);

        if ($res) $this->success(array('aid'=>$res['id'],'answer'=>$res['answer']));
        else $this->error();
    }
    public function updateAnswer()
    {
        $answer = $this->input->input_stream('answer');
        $aid = $this->input->input_stream('aid');

        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        $data = array('answer'=>$answer,'changed'=>time());
        $res = $this->commwap_model->updateAnswer($aid,$data);

        if ($res) $this->success();
        else $this->error();
    }
    public function addAnswer()
    {
        $answer = $this->input->input_stream('answer');
        $qid = $this->input->input_stream('qid');
        $uid = $this->input->input_stream('uid');
        if (!isset($qid)||!is_numeric($qid)) $this->error('error: lack question id');
        else $qid = (int)$qid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;

        $data = array('uid'=>$uid,'qid'=>$qid,'answer'=>$answer,'score'=>0,'created'=>time(),'changed'=>time());

        $res = $this->commwap_model->addAnswer($data);
        if ($res!==false) $this->success($res);
        else $this->error();

    }

    public function index()
    {
        $post = $this->input->post();
        if (!isset($post['aid'])||!is_numeric($post['aid'])) $this->error('error: lack answer id');
        else $aid = (int)$post['aid'];
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $res = $this->commwap_model->getAWithUserById($aid,$uid);
        $this->success($res);
    }

    public function getComment()
    {
        $post = $this->input->post();
        if (!isset($post['aid'])||!is_numeric($post['aid'])) $this->error('error: lack question id');
        else $aid = (int)$post['aid'];

        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        if (isset($post['sort'])) $sort = $post['sort'];
        else $sort = 'score';

        $res = $this->commwap_model->getCommentsByaid($aid,$offset,$limit,$sort,$uid);
        $this->success(array('comment'=>$res['comment'],'end'=>$res['end']));
    }

    public function agreeAnswer()
    {
        $aid = $this->input->input_stream('aid');
        $uid = $this->input->input_stream('uid');
        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->agreeAnswer($aid,$uid);
        $this->success();
    }

    public function cancelagreeAnswer()
    {
        $aid = $this->input->input_stream('aid');
        $uid = $this->input->input_stream('uid');
        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->cancelagreeAnswer($aid,$uid);
        $this->success();
    }

    public function collectAnswer()
    {
        $aid = $this->input->input_stream('aid');
        $uid = $this->input->input_stream('uid');
        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->collectAnswer($aid,$uid);
        $this->success();
    }

    public function cancelcollectAnswer()
    {
        $aid = $this->input->input_stream('aid');
        $uid = $this->input->input_stream('uid');
        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->cancelcollectAnswer($aid,$uid);
        $this->success();
    }

    public function addComment()
    {
        $aid = $this->input->input_stream('aid');
        $uid = $this->input->input_stream('uid');
        $comment = $this->input->input_stream('comment');
        if (!isset($aid)||!is_numeric($aid)) $this->error('error: lack answer id');
        else $aid = (int)$aid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;

        $data = array('uid'=>$uid,'answerid'=>$aid,'comments'=>$comment,'score'=>0,'created'=>time());

        if ($this->commwap_model->addComment($data)!==false) $this->success();
        else $this->error();
    }
}